/**   
  * @Title: MemoryListener.java 
  * @package com.sysmanage.permissions.listener 
  * @Description: 
  * @author XXXX@163.com
  * @date 2011-10-2 上午9:56:33 
  * @version V1.0   
  */

package com.sysmanage.permissions.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sysmanage.common.tools.json.Dic;
import com.sysmanage.common.tools.util.AppParamMemoryUtils;
import com.sysmanage.common.tools.util.ArgumentMemoryUtils;
import com.sysmanage.common.tools.util.DictionaryMemoryUtils;
import com.sysmanage.permissions.ConstCode;
import com.sysmanage.permissions.dao.AppOtherMemoryTableDefineDao;
import com.sysmanage.permissions.dao.ArgumentsDao;
import com.sysmanage.permissions.dao.CompanyDao;
import com.sysmanage.permissions.dao.DictionaryDao;
import com.sysmanage.permissions.dao.DimTableQueryDao;
import com.sysmanage.permissions.dao.UsersDao;
import com.sysmanage.permissions.model.AppOtherMemoryTableDefine;
import com.sysmanage.permissions.model.Arguments;
import com.sysmanage.permissions.model.Company;
import com.sysmanage.permissions.model.Dictionarys;
import com.sysmanage.permissions.model.Users;


/** 
  * @ClassName: MemoryListener 
  * @Description: 初始化全局参数和系统字典 
  * @author XXXX@163.com 
  * @date 2011-10-6 下午3:03:16 
  *  
  */
public class MemoryListener implements ServletContextListener {
	//系统字典Service层
	private DictionaryDao dictionaryDao;
	//全局参数
	private ArgumentsDao argumentsDao;
	
	
	
	private CompanyDao companyDao;
	
	private UsersDao userDao;
	
	private AppOtherMemoryTableDefineDao appOtherMemoryTableDefineDao;
	
	private DimTableQueryDao dimTableQueryDao;
	
	private ArgumentMemoryUtils argumentUtils = ArgumentMemoryUtils.getInstance();
	private DictionaryMemoryUtils dictionaryUtils = DictionaryMemoryUtils.getInstance();
	private AppParamMemoryUtils appParamUtils = AppParamMemoryUtils.getInstance();
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("tomcat 开始结束");
		dictionaryUtils.getDictionarysMap().clear();
		System.out.println(dictionaryUtils.getDictionarysMap() + "======================");
		argumentUtils.getArgumentsMap().clear();
		System.out.println(argumentUtils.getArgumentsMap() + "========================");
		appParamUtils.getParamCodeMap().clear();
		System.out.println(appParamUtils.getParamCodeMap() + "========================");
		
		
		System.out.println("tomcat 结束完毕");
	}

	public void contextInitialized(ServletContextEvent arg0) {
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
		
		this.setDictionaryDao((DictionaryDao)ctx.getBean("dictionaryDao"));
		this.setArgumentsDao((ArgumentsDao)ctx.getBean("argumentsDao"));
		this.setCompanyDao((CompanyDao)ctx.getBean("companyDao"));
		this.setUserDao((UsersDao)ctx.getBean("userDao"));
		this.setAppOtherMemoryTableDefineDao((AppOtherMemoryTableDefineDao)ctx.getBean("appOtherMemoryTableDefineDao"));
		this.setDimTableQueryDao((DimTableQueryDao)ctx.getBean("dimTableQueryDao"));
		System.out.println("tomcat缓存加载 开始");
		//系统字典读进内存
		List<String> nickNames = new ArrayList<String>();
		Map<String,Map<String,String>> dictionarysMap = new HashMap<String,Map<String,String>>();
		nickNames = dictionaryDao.getHandler().findListOfObj("SELECT distinct nickName from Dictionarys");//baseDAO.findListBySql("SELECT distinct nickname from c_dictionarys", Dictionarys.class);
		for(Iterator<String> iter=nickNames.iterator();iter.hasNext();){
			String nick =  iter.next();
			List<Dictionarys> dics = dictionaryDao.findList("FROM Dictionarys where nickName='"+nick+"'");
			Map<String,String> maps = new HashMap<String,String>();
			for(Iterator<Dictionarys> one=dics.iterator();one.hasNext();){
				Dictionarys d = one.next();
				maps.put(d.getValue(), d.getName());//Map<value,name>
			}
			dictionarysMap.put(nick, maps);
		}
		/*
		//把公司读入到字典表里面
		List<Company> companyList = companyDao.getAll();
		Map<String,String> copanyMaps = new HashMap<String,String>();
		for(Iterator<Company> one=companyList.iterator();one.hasNext();){
			Company d = one.next();
			copanyMaps.put(d.getId()+"", d.getCompanyName());//Map<value,name>
		}
		dictionarysMap.put(ConstCode.DIC_NICK_NAME_COMPANY, copanyMaps);
		
		//把用户读入到字典表里面
		List<Users> userList = userDao.getAll();
		Map<String,String> userMaps = new HashMap<String,String>();
		for(Iterator<Users> one=userList.iterator();one.hasNext();){
			Users d = one.next();
			userMaps.put(d.getId()+"", d.getName());//Map<value,name>
		}
		dictionarysMap.put(ConstCode.DIC_NICK_NAME_USER, userMaps);
		*/
		//把积分项参照读入字典表
		List<Dic> tmpList = null;
		List<AppOtherMemoryTableDefine> memoryTableList = appOtherMemoryTableDefineDao.getAllValidDefine();
		if(memoryTableList!=null&&memoryTableList.size()>0)
		{
			for(AppOtherMemoryTableDefine table:memoryTableList)
			{
				tmpList = dimTableQueryDao.getDicByTable(table.getTableName(), table.getFieldValue(), table.getFieldName(), table.getWhereCond());
				Map<String,String> dicMap = new HashMap<String,String>();
				for(Dic item:tmpList)
				{
					dicMap.put(item.getValue(),item.getName());
				}
				dictionarysMap.put(table.getRefDomain(),dicMap);
			}
		}
		
		dictionaryUtils.setDictionarysMap(dictionarysMap);
		
		
		
		//应用字典读进内存
		List<String> domainNames = new ArrayList<String>();
		Map<String,Map<String,String>> paramCodeMap = new HashMap<String,Map<String,String>>();
		for(Iterator<String> iter=domainNames.iterator();iter.hasNext();){
			String nick =  iter.next();

		}
		appParamUtils.setParamCodeMapMap(paramCodeMap);		
		//全局参数读进内存
		
		List<Arguments> list = new ArrayList<Arguments>();
		list = argumentsDao.findList("from Arguments");
		Map<String,String> argumentsMap = new HashMap<String ,String>();
		for(Iterator<Arguments> iter=list.iterator();iter.hasNext();){
			Arguments arg = iter.next();
			argumentsMap.put(arg.getName(), arg.getValue());
		}
		argumentUtils.setArgumentsMap(argumentsMap);
		
		System.out.println("tomcat 读入内存完毕!");
//		System.out.println(dictionaryUtils.getDictionarysMap() + "======================");
//		System.out.println(argumentUtils.getArgumentsMap() + "========================");
		
		System.out.println("tomcat 读入任务列表!");
		System.out.println("tomcat 读入任务列表完毕!");
	}

	/**
	 * @return the argumentsDao
	 */
	public ArgumentsDao getArgumentsDao() {
		return argumentsDao;
	}

	/**
	 * @param argumentsDao the argumentsDao to set
	 */
	public void setArgumentsDao(ArgumentsDao argumentsDao) {
		this.argumentsDao = argumentsDao;
	}

	/**
	 * @return the dictionaryDao
	 */
	public DictionaryDao getDictionaryDao() {
		return dictionaryDao;
	}

	/**
	 * @param dictionaryDao the dictionaryDao to set
	 */
	public void setDictionaryDao(DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}
	
	

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public UsersDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UsersDao userDao) {
		this.userDao = userDao;
	}

	public AppOtherMemoryTableDefineDao getAppOtherMemoryTableDefineDao() {
		return appOtherMemoryTableDefineDao;
	}

	public void setAppOtherMemoryTableDefineDao(
			AppOtherMemoryTableDefineDao appOtherMemoryTableDefineDao) {
		this.appOtherMemoryTableDefineDao = appOtherMemoryTableDefineDao;
	}

	public DimTableQueryDao getDimTableQueryDao() {
		return dimTableQueryDao;
	}

	public void setDimTableQueryDao(DimTableQueryDao dimTableQueryDao) {
		this.dimTableQueryDao = dimTableQueryDao;
	}

}
